﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;


namespace frm_view
{
    public partial class frmChuong : DevExpress.XtraEditors.XtraForm
    {
       
        
        private bool IsInsert = false;
        private void KhoaDieuKhien()
        {
            btnInsert.Enabled = true;
            btnDelete.Enabled = true;
            btnUpdate.Enabled = true;
            btnSave.Enabled = false;
            btnLink.Enabled = false;
            cbbMaMon.Enabled = false;

            txtMaChuong.Enabled = false;
            txtNoiDung.Enabled = false;
            txtTenChuong.Enabled = false;
            txtVideo.Enabled = false;
            btnLink.Enabled = true;
        }

        private void MoDieuKhien()
        {
            btnInsert.Enabled = false;
            btnDelete.Enabled = false;
            btnUpdate.Enabled = false;
            btnSave.Enabled = true;
            cbbMaMon.Enabled = true;

            txtMaChuong.Enabled = true;
            txtNoiDung.Enabled = true;
            txtTenChuong.Enabled = true;
            txtVideo.Enabled = true;
        }

        private void GetData()
        {

        }
        private void XoaText()
        {
            cbbMaMon.Text = "";
            txtMaChuong.Text = "";
            txtNoiDung.Text = "";
            txtTenChuong.Text = "";
            txtVideo.Text = "";
        }
        public frmChuong()
        {
            InitializeComponent();
        }

         private void ReFresh()
        {
            // TODO: This line of code loads data into the 'dsMonHoc.tblMonHoc' table. You can move, or remove it, as needed.
            this.tblMonHocTableAdapter.Fill(this.dsMonHoc.tblMonHoc);
            // TODO: This line of code loads data into the 'dsChap.tblChuong' table. You can move, or remove it, as needed.
            this.tblChuongTableAdapter.Fill(this.dsChap.tblChuong);
        }
        private void View_Chuong_Load(object sender, EventArgs e)
        {


            ReFresh();
            KhoaDieuKhien();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            XoaText();
            IsInsert = true;
            MoDieuKhien();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            IsInsert = false;
            MoDieuKhien();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            txtMaChuong.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[1]).ToString();
            if (XtraMessageBox.Show("Bạn đồng ý xóa", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (!string.IsNullOrEmpty(txtMaChuong.Text))
                {
                    tblChuongTableAdapter.DeleteQuery(txtMaChuong.Text);
                    XtraMessageBox.Show("Xóa Thành Công");

                }
                else
                    XtraMessageBox.Show("Chọn dữ liệu cần xóa");

            }
            else
                XtraMessageBox.Show("Chọn dữ liệu cần xóa");
            XoaText();
            Refresh();
            KhoaDieuKhien();
        }

        private void gripContent_Click(object sender, EventArgs e)
        {
            try
            {
                cbbMaMon.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]).ToString();
                txtMaChuong.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[1]).ToString();
                txtTenChuong.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[2]).ToString();
                txtNoiDung.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[3]).ToString();
                txtVideo.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[3]).ToString();
            }
            catch
            {

                return;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Error.ClearErrors();
            if (IsInsert == true)
            {
                if (string.IsNullOrEmpty(txtMaChuong.Text))
                    Error.SetError(txtMaChuong, "Nhập thông tin \"MÃ CHƯƠNG\"");
                else if (string.IsNullOrEmpty(txtTenChuong.Text))
                    Error.SetError(txtNoiDung, "Nhập thông tin \"NỘI DUNG\"");
                else if (string.IsNullOrEmpty(cbbMaMon.Text))
                    Error.SetError(txtNoiDung, "Lựa chọn \"MÔN\" Cần thiết lập");
                else
                {

                    try
                    {
                        tblChuongTableAdapter.Insert(cbbMaMon.Text, txtMaChuong.Text, txtTenChuong.Text, txtNoiDung.Text, txtVideo.Text);
                        XtraMessageBox.Show("Thêm Thành Công");
                        KhoaDieuKhien();

                    }
                    catch
                    {

                        XtraMessageBox.Show("Dữ Liệu Không Hợp Lệ");
                    }
                    ReFresh();
                    XoaText();
                }



            }

            else
            {
                if (string.IsNullOrEmpty(txtMaChuong.Text))
                    Error.SetError(txtMaChuong, "Nhập thông tin \"Mã Chương\"");
                else if (string.IsNullOrEmpty(txtTenChuong.Text))
                    Error.SetError(txtNoiDung, "Nhập thông tin \"NỘI DUNG\"");
                else if (string.IsNullOrEmpty(cbbMaMon.Text))
                    Error.SetError(txtNoiDung, "Lựa chọn \"MÔN\" Cần thiết lập");
                else
                {
                    tblChuongTableAdapter.UpdateQuery(cbbMaMon.Text, txtMaChuong.Text, txtTenChuong.Text, txtNoiDung.Text, txtVideo.Text, txtMaChuong.Text);
                    XtraMessageBox.Show("Cập nhật Thành Công");
                    ReFresh();
                    XoaText();
                    KhoaDieuKhien();
                }

            }

        }
        private void btnVideo_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
                txtVideo.Text = @"\" + ofd.FileName.Substring(ofd.FileName.LastIndexOf("\\") + 1);
        }
        private void btnLink_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "Text File (*.rtf)|*.rtf";
            if (ofd.ShowDialog() == DialogResult.OK)
                txtNoiDung.Text = @"\" + ofd.FileName.Substring(ofd.FileName.LastIndexOf("\\") + 1);
        }
    }
}